﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsMine.Storage;

namespace NewsMine.DataManagers
{
    /// <summary>
    /// This class is expected to deal with all kind of settings that can be used in application.
    /// Here settings can be stored in any form (clr objects - serializable) against unique key.
    /// 
    /// And also this class should have refeshing mechanism for each minute or some specific time interval.
    /// 
    /// </summary>
    public class SettingsManager
    {

        #region possible Keys

        public const string BlockedTitlesKey = "BlockedTitles";
        public const string BlockedImagesKey = "BlockedImages";
        public const string BlockedDomainsKey = "BlockedDomains";

        public const string FacebookAccountsKey = "fbaccounts";
        #endregion

        Dictionary<string, object> dictSettings = new Dictionary<string, object>();

        IKeyValueStore settingStore = Defaults.SettingsStore;

        List<string> listOfKeys = new List<string>();

        public SettingsManager()
        {
            listOfKeys.Add(BlockedTitlesKey);
            listOfKeys.Add(BlockedImagesKey);
            listOfKeys.Add(BlockedDomainsKey);
        }

        #region refreshing mechanism

        DateTime lastRetrievedTime = DateTime.MinValue;
        int refreshingInterval = 1; //minutes.

        void RefreshSettings()
        {
            if (lastRetrievedTime.AddMinutes(refreshingInterval) < DateTime.UtcNow)
            {
                ReadFreshSettings();
            }
        }

        private void ReadFreshSettings()
        {
            //todo: needs to read the values against keys;

            dictSettings.Clear();

            foreach (var key in listOfKeys)
            {
                var value = settingStore.Get(key);

                if (value == null) //that means first time, just go ahead add the default values.
                {
                    SetDefaultValuesInSettings();
                    ReadFreshSettings();
                    return;
                }
                else
                    dictSettings[key] = value;
            }

            lastRetrievedTime = DateTime.UtcNow;
        }

        private void SetDefaultValuesInSettings()
        {
            settingStore.Open();
             settingStore.Replace(BlockedTitlesKey,new List<string>());
             settingStore.Replace(BlockedImagesKey,new List<string>());
             settingStore.Replace(BlockedDomainsKey,new List<string>());
             settingStore.Close();
        }

        #endregion

        public object GetSettingsByKey(string key)
        {
            RefreshSettings();

            if (dictSettings.Keys.Contains(key))
                return dictSettings[key];
            else
                return null;
        }

        public void SaveSettings(string key, object value)
        {
            settingStore.Set(key, value);

            RefreshSettings();
        }

    }
}



    

